package cn.ynu.cmrs.activity;

import cn.ynu.cmrs.R;
import cn.ynu.cmrs.MyApplication;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.media.MediaPlayer.*;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

/**
 * Activity play videos from stream server
 * 
 * @author Yasha
 *
 */
public class VideoPlayerActivity extends Activity {

	private VideoView vv;
	private int pausePosition = -1;
	private ProgressDialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		MyApplication.allActivity.add(this);
		setContentView(R.layout.videoplay);
		dialog = ProgressDialog.show(this,
				getResources().getString(R.string.app_name),
				getString(R.string.video_loading));

		vv = (VideoView) findViewById(R.id.videoView);
		vv.setMediaController(new MediaController(this));
		vv.setVideoURI(Uri.parse(getIntent().getStringExtra("videoUrl")));
		vv.start();

		vv.setOnPreparedListener(new OnPreparedListener() {
			@Override
			public void onPrepared(MediaPlayer mp) {
				// TODO Auto-generated method stub
				vv.setBackgroundColor(Color.argb(0, 0, 255, 0));
				dialog.dismiss();
			}
		});
		vv.setOnCompletionListener(new OnCompletionListener() {

			@Override
			public void onCompletion(MediaPlayer mp) {
				// TODO Auto-generated method stub
				if (mp.isPlaying()) {
					vv.seekTo(0);
					Toast.makeText(VideoPlayerActivity.this,
							R.string.video_end, 2).show();
				}
			}
		});

		vv.setOnErrorListener(new OnErrorListener() {

			@Override
			public boolean onError(MediaPlayer mp, int what, int extra) {
				// TODO Auto-generated method stub
				//Load the video maybe encounter an error, remove the loading progress bar here
				if (dialog.isShowing()) {
					dialog.dismiss();
				}
				new AlertDialog.Builder(VideoPlayerActivity.this)
						.setTitle(R.string.fail_general).setMessage(R.string.video_play_fail)
						.setPositiveButton(R.string.ok, new OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								VideoPlayerActivity.this.finish();
							}
						}).show();
				return true;
			}
		});
	}
	
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		MyApplication.allActivity.remove(this);
		this.finish();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		pausePosition = vv.getCurrentPosition();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (pausePosition > 0) {
			vv.seekTo(pausePosition);
		}
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		pausePosition = vv.getCurrentPosition();
	}

	/**
	 * Launch this Activity from the outside
	 *
	 * @param context context from which Activity should be started
	 */
	public static void launch(Context context) {
		// TODO Auto-generated method stub
		Intent intent = new Intent(context, VideoPlayerActivity.class);
		context.startActivity(intent);
	}

}
